import axios from 'axios'
import store from '@/store'
// import jsonBig from 'json-bigint'
// import router from '@/router'
// /
import JSONBig from 'json-bigint'

//

const request = axios.create({
  // 接口的基准路径
  baseURL: 'http://toutiao-app.itheima.net',
  transformResponse: [function (data) {
    try {
      return JSONBig.parse(data)
    } catch (err) {
      return data
    }

    // axios 默认会在内部这样来处理后端返回的数据
    // return JSON.parse(data)
  }]
})

// 请求拦截器
request.interceptors.request.use(function (config) {
  if (config.url.startsWith('/app')) {
    config.url = config.url.slice(4)
  }

  const { user } = store.state
  if (user && user.token) {
    config.headers.Authorization = `Bearer ${user.token}`
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

export default request
